package com.example.demo.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * description： <br>
 * createTime: 2023/7/1811:21 <br>
 *
 * @author zzh
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    HttpServletRequest request;

    @RequestMapping("/demo")
    public Map<String, Object> demo() throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        int available = inputStream.available();
        if(available > 0) {
            byte[] bytes = new byte[189];
            IOUtils.readFully(inputStream, bytes);
            System.out.println(new String(bytes));
        }
        System.out.println(request.getQueryString());
        System.out.println(request.getParameterMap());
        System.out.println("------------------------------");
        System.out.println(request.getParameter("name"));
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", null);
        return result;
    }

    @RequestMapping("/test")
    public Map<String, Object> demo(MultipartFile file, MultipartFile[] files) throws ServletException, IOException {
        Enumeration<String> headerNames = request.getHeaderNames();
        System.out.println("==========================");
        while(headerNames.hasMoreElements()) {
            String s = headerNames.nextElement();
            System.out.println(s + request.getHeader(s));
        }
        System.out.println("file" + file);
        System.out.println("files" + files);
        System.out.println(request.getQueryString());
        System.out.println("request.getParameterMap().size()" + request.getParameterMap().size());
        System.out.println("request.getParts().size()" + request.getParts().size());
        System.out.println(request.getInputStream().available());
        System.out.println("------------------------------");
        System.out.println(request.getParameter("name"));
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("data", null);
        return result;
    }

    @RequestMapping("/test123")
    public String test123(MultipartFile file) {

        String url = "http://20.26.211.69:10103/vueReq/com.ai.pcestudio.vue.fusion.action.OfferVasFusionAction?action=updatePicAll&"
                        + request.getQueryString();//接口地址
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("multipartFile", file.getResource());
        String str = restTemplate.postForObject(url, param, String.class);
        System.out.println(str);
        return str;
    }


    @RequestMapping("/test456/{id}")
    public String test456(@PathVariable("id") Object id) {

        System.out.println(id);
        return "ok";
    }
}
